package core;

import java.lang.Exception;

public class PrefrenceLoader {

	public static Object[] setupPrefs(Object[] prefs, Object[] defPrefs, Object[] cliPrefs, int len) throws Exception{
		Object[] ret = new Object[len];
		if(prefs.length < len || defPrefs.length < len || cliPrefs.length < len){
			throw new Exception("prefs/defPrefs/cliPrefs length too short.");
		}
		for(int i=0; i<len; i++){
			//set it to our preference
			ret[i] = prefs[i];
			//if a cli preference overrides, use it.
			if(cliPrefs[i] != null){
				ret[i] = cliPrefs[i];
			}
			//if there is no preference, use default.
			if(ret[i] == null){
				ret[i] = defPrefs[i];
			}
		}
		return ret;
	}

	/**
	 * 
	 * @param def default prefrences to load
	 * @param loadPrefs first to last ([0] -> [x]) the prefrences to be loaded
	 * @return returns end result of prefrence loading
	 */
	public static Object[] setupPrefs(Object[] def, Object[][] loadPrefs){
		int min = Integer.MAX_VALUE;
		for(Object[] o : loadPrefs){
			min = Math.min(o.length,min);
		}
		Object[] ret = new Object[min];
		for(int i=0; i<min; i++){
			ret[i] = def[i];
		}

		for(int i=0; i<loadPrefs.length; i++){

			for(int j=0; j<min; j++){
				ret[i] = loadPrefs[i][j];
			}

		}
		return ret;
	}

}
